package yt.kpool;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.activity.personal.LoginActivity;
import yt.kpool.activity.salesman.SalesmanMainActivity;
import yt.kpool.adapter.PersonSelectListviewAdapter;
import yt.kpool.base.BaseApplication;
import yt.kpool.bean.ChildAccountBean;
import yt.kpool.bean.CurrencyList;
import yt.kpool.bean.VersionBean;
import yt.kpool.fragment.HomePageFragment;
import yt.kpool.fragment.MinerFragment;
import yt.kpool.fragment.OrePoolFragment;
import yt.kpool.fragment.ProfitFragment;
import yt.kpool.fragment.SetFragment;
import yt.kpool.interfac.OnSelectPersonListener;
import yt.kpool.interfac.WechatServer;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.OtherUitl;
import yt.kpool.util.SPUtil;
import yt.kpool.util.ToastUtil;
import yt.kpool.util.imageUtil.GlideUtil;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.dialog.UpdateDialog;
import yt.kpool.view.lazyviewpager.LazyFragmentPagerAdapter;
import yt.kpool.view.lazyviewpager.LazyViewPager;

public class MainActivity extends AppCompatActivity implements OnSelectPersonListener {
    @BindView(R.id.viewpager)
    LazyViewPager viewpager;
    @BindView(R.id.iv_homepage)
    ImageView iv_homepage;
    @BindView(R.id.iv_orePool)
    ImageView iv_orePool;
    @BindView(R.id.iv_profit)
    ImageView iv_profit;
    @BindView(R.id.iv_set)
    ImageView iv_set;
    @BindView(R.id.iv_miner)
    ImageView iv_miner;
    @BindView(R.id.tv_homepage)
    TextView tv_homepage;
    @BindView(R.id.tv_orePool)
    TextView tv_orePool;
    @BindView(R.id.tv_miner)
    TextView tv_miner;
    @BindView(R.id.tv_profit)
    TextView tv_profit;
    @BindView(R.id.tv_set)
    TextView tv_set;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawer_layout;
    @BindView(R.id.lv_person)
    ListView lv_person;//抽屉账户列表
    public PersonSelectListviewAdapter personSelectListviewAdapter;//抽屉账户列表
    private List<ChildAccountBean.DataBean> personData;

    private CustomLazyFragmentPagerAdapter customLazyFragmentPagerAdapter;
    private WechatServer wechatServer;//被观察者


    private SPUtil spUtil;

    private String childAccount;//子账户名字
    private int childAccountid;//子账户id
    private int type = 0;//0正常 1合计

    private int currencyid;//币种id


    public static final int TOTALID = 0;

    public boolean IsFastLogin = false;//是否是快速登录

    private LoadingDialog loadingDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        loadingDialog=new LoadingDialog(this,0);
        initview();
        initPersonList();

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        SPUtil spUtil2 = new SPUtil(BaseApplication.context, BaseApplication.KEYLOGIN);
        if (!"".equals(spUtil2.getString(BaseApplication.TOKEN, ""))) {
            //快速登录
            String id = spUtil2.getString(BaseApplication.ChildAccountId, "0");
            String name = spUtil2.getString(BaseApplication.ChildAccountName, "");
            childAccount = name;
            childAccountid = Integer.valueOf(id);
            IsFastLogin = true;
        }else {
           if(!"".equals(spUtil.getString(BaseApplication.TOKEN, ""))){
               //已登录
               getCurrrencyData();
           }
        }


        getVersionInfo();

        initGuestFast();


    }

    /**
     * 客户快速登录
     */
    private void initGuestFast() {
        if ("GC".equals( getIntent().getStringExtra("From"))){
            if (IsFastLogin) {
                //快捷登录

                setBottomLabState(1);
                int p = getSupportFragmentManager().getFragments().size();
                viewpager.setCurrentItem(1, false);
                // 禁止手势滑动
                drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

                if (getSupportFragmentManager().getFragments().size() > p) {
                    wechatServer.registerObserver((OrePoolFragment) getSupportFragmentManager().getFragments().get(p));
                }
                personSelectListviewAdapter.setTotal(true);
                EventBus.getDefault().post("123");
            } else {


                String id = spUtil.getString(BaseApplication.ID, "0");
                if ("0".equals(id)) {
                    //未登录
                    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                    intent.putExtra("type", 1);
                    startActivityForResult(intent, 50);
                } else {

                    setBottomLabState(1);
                    int p = getSupportFragmentManager().getFragments().size();
                    viewpager.setCurrentItem(1, false);
                    // 打开手势滑动
                    drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
                    if (getSupportFragmentManager().getFragments().size() > p) {
                        wechatServer.registerObserver((OrePoolFragment) getSupportFragmentManager().getFragments().get(p));
                    }
                    personSelectListviewAdapter.setTotal(true);
                    EventBus.getDefault().post("123");
                }
            }
        }
    }

    /**
     * 获取子账户
     */
    private void getChildAccount(boolean isLoading) {
        if (NetWorkUtil.check(this)) {
           if (isLoading)
               loadingDialog.show();
            NetHelperNew.GetChildAccount(currencyid + "", new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    loadingDialog.dismiss();
                    Log.i("-->", "子账户列表：" + data);
                    ChildAccountBean childAccountBean = MyGson.getInstance().fromJson(data, ChildAccountBean.class);
                    if (childAccountBean.getType() == 1) {
                        personData.clear();
                        personData.addAll(childAccountBean.getData());
                        personSelectListviewAdapter.notifyDataSetChanged();

                        //往fragment发送消息
                        if (personData.size() > 0) {
                            if (childAccountid == 0) {
                                //第一次请求
                                childAccount = personData.get(0).getChildAccountName();
                                childAccountid = personData.get(0).getChildAccountId();
                                type = 1;
                                wechatServer.setInfomation(0, 1, personData.get(0).getChildAccountName(), personData.get(0).getChildAccountId());
                                personSelectListviewAdapter.setSelected(personData.get(0).getChildAccountId());
                            }

                        } else {
                            type = 1;
                            childAccount="";
                            childAccountid=0;
                            wechatServer.setInfomation(0, 1, "", 0);
                        }


                        if (type == 1) {
                            if (viewpager.getCurrentItem() == 1) {
                                //合计
                                personSelectListviewAdapter.setSelected(MainActivity.TOTALID);
                            } else {
                                if (personData.size() > 0) {
                                    personSelectListviewAdapter.setSelected(personData.get(0).getChildAccountId());
                                }

                            }

                        }


                        if (viewpager.getCurrentItem() == 1) {
                            Log.i("-->", "矿池页展开");
                            personSelectListviewAdapter.setTotal(true);
                        } else {
                            Log.i("-->", "矿池页其他页展开");
                            personSelectListviewAdapter.setTotal(false);
                        }
                    } else {
                        new ErrorDialog(MainActivity.this, childAccountBean.getContent()).show();
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();

                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });


        }


    }


    /**
     * 初始化
     */
    private void initview() {

        setBottomLabState(0);
        // 禁止手势滑动
        drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        customLazyFragmentPagerAdapter = new CustomLazyFragmentPagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(customLazyFragmentPagerAdapter);


        wechatServer = new WechatServer();
        spUtil = new SPUtil(BaseApplication.context, BaseApplication.FILENAME);
        childAccount = "";
    }

    /**
     * 底部类别点击事件处理
     *
     * @param view
     */
    @OnClick({R.id.ly_homepage, R.id.ly_orePool, R.id.ly_miner, R.id.ly_profit, R.id.ly_set, R.id.tv_downlogin})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ly_homepage://首页
                setBottomLabState(0);
                viewpager.setCurrentItem(0, false);
                // 禁止手势滑动
                drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                break;
            case R.id.ly_orePool://矿池

                if (IsFastLogin) {
                    //快捷登录

                    setBottomLabState(1);
                    int p = getSupportFragmentManager().getFragments().size();
                    viewpager.setCurrentItem(1, false);
                    // 禁止手势滑动
                    drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                    if (getSupportFragmentManager().getFragments().size() > p) {
                        wechatServer.registerObserver((OrePoolFragment) getSupportFragmentManager().getFragments().get(p));
                    }
                    personSelectListviewAdapter.setTotal(true);
                    EventBus.getDefault().post("123");
                } else {


                    String id = spUtil.getString(BaseApplication.ID, "0");
                    if ("0".equals(id)) {
                        //未登录
                        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                        intent.putExtra("type", 1);
                        startActivityForResult(intent, 50);
                    } else {

                        setBottomLabState(1);
                        int p = getSupportFragmentManager().getFragments().size();
                        viewpager.setCurrentItem(1, false);
                        // 打开手势滑动
                        drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
                        if (getSupportFragmentManager().getFragments().size() > p) {
                            wechatServer.registerObserver((OrePoolFragment) getSupportFragmentManager().getFragments().get(p));
                        }
                        personSelectListviewAdapter.setTotal(true);
                        EventBus.getDefault().post("123");
                    }
                }

                break;
            case R.id.ly_miner://旷工
                if (IsFastLogin) {
                    //快捷登录
                    setBottomLabState(2);
                    int s = getSupportFragmentManager().getFragments().size();
                    viewpager.setCurrentItem(2, false);
                    // 禁止手势滑动
                    drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                    if (getSupportFragmentManager().getFragments().size() > s) {
                        wechatServer.registerObserver((MinerFragment) getSupportFragmentManager().getFragments().get(s));
                    }
                    personSelectListviewAdapter.setTotal(false);
                    EventBus.getDefault().post("222");
                } else {


                    String id2 = spUtil.getString(BaseApplication.ID, "0");
                    if ("0".equals(id2)) {
                        //未登录
                        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                        intent.putExtra("type", 1);
                        startActivityForResult(intent, 50);
                    } else {
                        setBottomLabState(2);
                        int s = getSupportFragmentManager().getFragments().size();
                        viewpager.setCurrentItem(2, false);
                        // 打开手势滑动
                        drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
                        if (getSupportFragmentManager().getFragments().size() > s) {
                            wechatServer.registerObserver((MinerFragment) getSupportFragmentManager().getFragments().get(s));
                        }
                        personSelectListviewAdapter.setTotal(false);
                        EventBus.getDefault().post("222");
                    }
                }
                break;
            case R.id.ly_profit://收益

                if (IsFastLogin) {
                    //快捷登录
                    setBottomLabState(3);
                    int t = getSupportFragmentManager().getFragments().size();
                    viewpager.setCurrentItem(3, false);
                    // 禁止手势滑动
                    drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                    if (getSupportFragmentManager().getFragments().size() > t) {
                        wechatServer.registerObserver((ProfitFragment) getSupportFragmentManager().getFragments().get(t));
                    }
                    personSelectListviewAdapter.setTotal(false);
                    EventBus.getDefault().post("456");
                } else {


                    String id3 = spUtil.getString(BaseApplication.ID, "0");
                    if ("0".equals(id3)) {
                        //未登录
                        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                        intent.putExtra("type", 1);
                        startActivityForResult(intent, 50);
                    } else {
                        setBottomLabState(3);
                        int t = getSupportFragmentManager().getFragments().size();
                        viewpager.setCurrentItem(3, false);
                        // 打开手势滑动
                        drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
                        if (getSupportFragmentManager().getFragments().size() > t) {
                            wechatServer.registerObserver((ProfitFragment) getSupportFragmentManager().getFragments().get(t));
                        }
                        personSelectListviewAdapter.setTotal(false);
                        EventBus.getDefault().post("456");
                    }
                }
                break;
            case R.id.ly_set://设置
                setBottomLabState(4);

                int s = getSupportFragmentManager().getFragments().size();
                viewpager.setCurrentItem(4, false);
                // 禁止手势滑动
                drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                if (getSupportFragmentManager().getFragments().size() > s) {
                    wechatServer.registerObserver((SetFragment) getSupportFragmentManager().getFragments().get(s));
                }
                break;
            case R.id.tv_downlogin://退出登录

                //清除数据

                spUtil.remove(BaseApplication.TOKEN);
                spUtil.remove(BaseApplication.NAME);
                spUtil.remove(BaseApplication.ID);
                setBottomLabState(0);
                viewpager.setCurrentItem(0, false);
                // 禁止手势滑动
                drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                childAccountid = 0;

                break;
        }
    }


    /**
     * 点击的底部分类图标状态
     *
     * @param position
     */
    private void setBottomLabState(int position) {
        switch (position) {
            case 0:
                setTextAndImg(tv_homepage, iv_homepage, R.mipmap.icon_homepre, true);
                setTextAndImg(tv_orePool, iv_orePool, R.mipmap.icon_pool, false);
                setTextAndImg(tv_miner, iv_miner, R.mipmap.icon_miner, false);
                setTextAndImg(tv_profit, iv_profit, R.mipmap.icon_profit, false);
                setTextAndImg(tv_set, iv_set, R.mipmap.icon_set, false);
                break;
            case 1:
                setTextAndImg(tv_homepage, iv_homepage, R.mipmap.icon_home, false);
                setTextAndImg(tv_orePool, iv_orePool, R.mipmap.icon_poolpre, true);
                setTextAndImg(tv_miner, iv_miner, R.mipmap.icon_miner, false);
                setTextAndImg(tv_profit, iv_profit, R.mipmap.icon_profit, false);
                setTextAndImg(tv_set, iv_set, R.mipmap.icon_set, false);
                break;
            case 2:
                setTextAndImg(tv_homepage, iv_homepage, R.mipmap.icon_home, false);
                setTextAndImg(tv_orePool, iv_orePool, R.mipmap.icon_pool, false);
                setTextAndImg(tv_miner, iv_miner, R.mipmap.icon_minerpre, true);
                setTextAndImg(tv_profit, iv_profit, R.mipmap.icon_profit, false);
                setTextAndImg(tv_set, iv_set, R.mipmap.icon_set, false);
                break;
            case 3:
                setTextAndImg(tv_homepage, iv_homepage, R.mipmap.icon_home, false);
                setTextAndImg(tv_orePool, iv_orePool, R.mipmap.icon_pool, false);
                setTextAndImg(tv_miner, iv_miner, R.mipmap.icon_miner, false);
                setTextAndImg(tv_profit, iv_profit, R.mipmap.icon_profitpre, true);
                setTextAndImg(tv_set, iv_set, R.mipmap.icon_set, false);
                break;
            case 4:
                setTextAndImg(tv_homepage, iv_homepage, R.mipmap.icon_home, false);
                setTextAndImg(tv_orePool, iv_orePool, R.mipmap.icon_pool, false);
                setTextAndImg(tv_miner, iv_miner, R.mipmap.icon_miner, false);
                setTextAndImg(tv_profit, iv_profit, R.mipmap.icon_profit, false);
                setTextAndImg(tv_set, iv_set, R.mipmap.icon_setpre, true);
                break;

        }
    }


    /**
     * 设置选择分类的样式
     *
     * @param tv
     * @param iv
     * @param res
     * @param isBlue
     */
    private void setTextAndImg(TextView tv, ImageView iv, int res, boolean isBlue) {
        iv.setImageResource(res);
        if (isBlue) {
            tv.setTextColor(ContextCompat.getColor(this, R.color.system_color));
        } else {
            tv.setTextColor(ContextCompat.getColor(this, R.color.black1));
        }
    }

    /**
     * 初始化子账户
     */
    private void initPersonList() {


        personData = new ArrayList<>();
        personSelectListviewAdapter = new PersonSelectListviewAdapter(this, personData);
        lv_person.setAdapter(personSelectListviewAdapter);

        personSelectListviewAdapter.notifyDataSetChanged();
        lv_person.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                personSelectListviewAdapter.setSelected(personData.get(i).getChildAccountId());
                //往fragment发送消息
                type = 0;
                childAccountid = personData.get(i).getChildAccountId();
                childAccount = personData.get(i).getChildAccountName();
                Log.i("-->", "点击第" + i + "条 " + "名称：" + personData.get(i).getChildAccountName() + " id:" + personData.get(i).getChildAccountId());

                if (personData.get(i).getChildAccountId() == TOTALID) {
                    type = 1;
                    //选择合计
                    if (personData.size() > 2) {

                        wechatServer.setInfomation(0, 1, personData.get(1).getChildAccountName(), personData.get(1).getChildAccountId());
                    }

                } else {
                    if (viewpager.getCurrentItem()==1)
                    {
                        //矿池页
                        wechatServer.setInfomation(0, 6, personData.get(i).getChildAccountName(), personData.get(i).getChildAccountId());
                    }else if (viewpager.getCurrentItem()==3)
                    {
                        //收益页
                        wechatServer.setInfomation(0, 7, personData.get(i).getChildAccountName(), personData.get(i).getChildAccountId());
                    }else {
                        wechatServer.setInfomation(0, 0, personData.get(i).getChildAccountName(), personData.get(i).getChildAccountId());
                    }


                }

                drawer_layout.closeDrawers();
            }
        });

        drawer_layout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View view, float v) {


            }

            @Override
            public void onDrawerOpened(@NonNull View view) {

                for (int i = 0; i < currencyAllList.size(); i++) {
                    if (currencyAllList.get(i).getId()==currencyid)
                    {

                        personSelectListviewAdapter.setBlanceunit(currencyAllList.get(i).getCode().toUpperCase());
                    }
                }

                getChildAccount(false);

            }


            @Override
            public void onDrawerClosed(@NonNull View view) {
                personData.clear();
                personSelectListviewAdapter.notifyDataSetChanged();
            }

            @Override
            public void onDrawerStateChanged(int i) {

            }
        });

    }

    /**
     * 子账户选择  初始化账户列表
     *
     * @param type
     * @param id   账户
     */
    @Override
    public void selectPersonLayout(int type, int id) {
        if (type == 6) {
            //退出登录
            //清除数据
            spUtil.remove(BaseApplication.TOKEN);
            spUtil.remove(BaseApplication.NAME);
            spUtil.remove(BaseApplication.ID);
            setBottomLabState(0);
            viewpager.setCurrentItem(0, false);
            // 禁止手势滑动
            drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            childAccountid = 0;
        } else if (type == 5) {
            //返回正常登录操作
            IsFastLogin = false;
//            //快速登录返回
//            setBottomLabState(0);
//            viewpager.setCurrentItem(0, false);
            SPUtil spUtilkey = new SPUtil(BaseApplication.context, BaseApplication.KEYLOGIN);
            //清除数据
            spUtilkey.clear();
//            // 禁止手势滑动
//            drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
//            if (!"-1".equals(new SPUtil(BaseApplication.context, BaseApplication.FILENAME).getString(BaseApplication.ID, "-1"))) {
//                childAccountid=0;
////              getChildAccount(false);
//                getCurrrencyData();
//            }

            if (spUtil.getInt(BaseApplication.UserType,0)==5)
            {
                //返回业务员页面
                //业务员
                Intent intent = new Intent(MainActivity.this, SalesmanMainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }else {
                Intent intent = new Intent(MainActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }




        } else {
            if (!IsFastLogin) {
                drawer_layout.openDrawer(Gravity.LEFT);
                personSelectListviewAdapter.setSelected(id);
            }
        }


    }

    /**
     * 获取子账户名称
     *
     * @return
     */
    @Override
    public String childAccountName() {
        return childAccount;
    }

    /**
     * 获取子账户id
     *
     * @return
     */
    @Override
    public int childAccountId() {
        return childAccountid;
    }

    /**
     * @return 获取子账户type
     */
    @Override
    public int getchildAccountType() {
        return type;
    }


    /**
     * 获取币种id
     *
     * @param id
     */
    @Override
    public void selectCurrency(int id) {
        currencyid = id;
        wechatServer.setInfomation(1, 0, "", id);
        for (int i = 0; i < currencyAllList.size(); i++) {
            if (currencyAllList.get(i).getId()==currencyid)
            {

                personSelectListviewAdapter.setBlanceunit(currencyAllList.get(i).getCode().toUpperCase());
            }
        }

        getChildAccount(false);
    }

    @Override
    public int getCurrencyId() {
        return currencyid;
    }

    @Override
    public boolean getIsFastLogin() {
        return IsFastLogin;
    }

    private static class CustomLazyFragmentPagerAdapter extends LazyFragmentPagerAdapter {

        private CustomLazyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(ViewGroup container, int position) {
            if (position == 0) {
                return new HomePageFragment();
            } else if (position == 1) {
                return new OrePoolFragment();
            } else if (position == 2) {
                return new MinerFragment();
            } else if (position == 3) {
                return new ProfitFragment();
            } else if (position == 4) {
                return new SetFragment();
            }
            return null;
        }

        @Override
        public int getCount() {
            return 5;
        }

    }

    /**
     * 返回键隐藏到后台
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dialog();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /*退出对话框*/
    public void dialog() {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("提示")
                .setMessage("确定要退出吗？")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
//                        Intent intent = new Intent(Intent.ACTION_MAIN);
//                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                        intent.addCategory(Intent.CATEGORY_HOME);
//                        startActivity(intent);
                        finish();
                    }
                })
                .setNegativeButton("否", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        AlertDialog dialog_dialog = dialog.create();
        dialog_dialog.show();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private List<CurrencyList.DataBean> currencyAllList=new ArrayList<>();//所有币种列表
    /**
     * 获取币种
     */
    private void getCurrrencyData() {

        if (NetWorkUtil.check(this)) {
            loadingDialog.show();

            NetHelperNew.GetCurrencyList(new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    loadingDialog.dismiss();
                    Log.i("-->", "Main币种列表：" + data);
                    CurrencyList currencyList = MyGson.getInstance().fromJson(data, CurrencyList.class);
                    if (currencyList.getType() == 1) {

                        if (currencyList.getData().size()>0)
                        {
                            currencyAllList.clear();
                            currencyAllList.addAll(currencyList.getData());


                            currencyid=currencyList.getData().get(0).getId();
                            getChildAccount(true);
                        }


                    } else {
                        ToastUtil.showToast(MainActivity.this, currencyList.getContent());

                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();

                    ToastUtil.showToast(MainActivity.this, msg);
                }
            });


        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 10) {
            Log.i("-->", "登录返回=====================");
            setBottomLabState(0);
            viewpager.setCurrentItem(0, false);
            // 禁止手势滑动
            drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

            //登录回来请求数据
            getCurrrencyData();


        }

        if (resultCode == 5) {
//            getChildAccount();
            Log.i("-->", "挖币账户返回的=====================");
        }
        if (resultCode == 66) {
            //快捷登录返回
            SPUtil spUtil2 = new SPUtil(BaseApplication.context, BaseApplication.KEYLOGIN);
            if (!"".equals(spUtil2.getString(BaseApplication.TOKEN, ""))) {
                IsFastLogin = true;
                //快速登录返回
                setBottomLabState(0);
                viewpager.setCurrentItem(0, false);
                // 禁止手势滑动
                drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);


                String id = spUtil2.getString(BaseApplication.ChildAccountId, "0");
                String name = spUtil2.getString(BaseApplication.ChildAccountName, "");
                childAccount = name;
                childAccountid = Integer.valueOf(id);
                wechatServer.setInfomation(0, 3, childAccount, childAccountid);
            }


        }
    }


    /**
     * 版本跟新
     */
    private void getVersionInfo() {
        NetHelperNew.GetVersionInfo(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {

                VersionBean baseBean = MyGson.getInstance().fromJson(data, VersionBean.class);
                if (baseBean.getType() == 1) {
                    getIsUpdate(baseBean.getData().getVersionCode(), baseBean.getData().getUpdateUrl(), baseBean.getData().getUpdateInfo());
                }

            }

            @Override
            public void onError(String msg) {
                super.onError(msg);

            }
        });

    }

    /**
     * 版本更新
     *
     * @param versioncode 版本号
     * @param url         下载地址
     */
    private void getIsUpdate(String versioncode, String url, String versioninfo) {
        if (versioncode.length() > 0)
            if (Integer.valueOf(versioncode) > Integer.valueOf(OtherUitl.getVersionCode(MainActivity.this))) {
                UpdateDialog updateDialog = new UpdateDialog(MainActivity.this, R.style.dialog, versioninfo, url);
                updateDialog.show();
            }
//            else {
//                ToastUtil.showToast(MainActivity.this, "当前已经是最新版本");
//            }


    }


}
